package com.study.mina;

import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

import java.net.InetSocketAddress;

/**
 * Description:
 * User: 27010
 * DateTime: 2018/5/13 10:53
 */
public class Client {
    private static final String ip="127.0.0.1";
    private static int port =  8080;
    public static void main(String[] args) {
        NioSocketConnector connector = new NioSocketConnector();

           //创建接受数据的过滤器
        DefaultIoFilterChainBuilder chain = connector.getFilterChain();

        //设定这个过滤器将一行一行(/r/n)的读取数据
        chain.addLast("myChain", new ProtocolCodecFilter(new TextLineCodecFactory()));

        //服务器的消息处理器：一个 SimpleMinaClientHandler 对象
        connector.setHandler(new SimpleClientHandler());
        //TODO 这种超时 是如何实现的
        connector.setConnectTimeoutMillis(200);

         //连接到服务器：
        ConnectFuture cf = connector.connect(new InetSocketAddress(ip,port));

        cf.awaitUninterruptibly();

        cf.getSession().getCloseFuture().awaitUninterruptibly();

        try {
            Thread.sleep(10000000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        connector.dispose();
    }
}
